import {
	get,
	post
} from '../../utils/request.js'
import {
	getOpenId
} from '../../utils/getOpenid.js'
Page({
	data: {
		cartList: [],
		result: [],
		listIds: [],
		checked: false,
		checkNumber: 0,
		checkPrice: 0,
		a: 0,
		disabled: true,
		topIconIsShow: false, // 控制滚动到top的icon的显示
	},
	onLoad: function(options) {
		this.getData()
	},
	onShow() {
		this.getData()
	},
	onTabItemTap() { // 点击tabbar触发的事件
		this.getData()
	},
	async getData() {
		const data = await get('/cart/cartList', {
			openId: wx.getStorageSync('OPENID') || ''
		})
		// console.log(data)
		this.setData({
			cartList: data.data
		})
	},
	onChange(e) {
		// console.log(e.detail)
		// console.log(e)
		this.setData({
			result: e.detail,
		})
		if (this.data.result.length == 0) {
			this.setData({
				checkPrice: 0,
				checkNumber: 0,
				checked: false,
				result: [],
				disabled: true
			})
		} else {
			this.setData({
				disabled: false
			})
		}
		// console.log(this.data.result)
		this.checkNumber()
		this.checkPrice()
	},
	onAllChange () {
		this.setData({
			result: []
		})
		if (this.data.checked) {
			this.setData({
				checked: false,
				checkNumber: 0,
				checkPrice: 0,
				disabled: true
			})
		} else {
			this.setData({
				checked: true,
				disabled: false
			})
			var arr = []
			for (let i=0;i<this.data.cartList.length;i++) {
				const ele = this.data.cartList[i].goods_id.toString()
				// console.log(ele)
				arr.push(ele)
				//console.log(arr)
				this.setData({
					result: arr
				})
			}
		}
		this.checkNumber()
		this.checkPrice()
	},
	checkNumber () {
		let number = 0
		for (let i=0;i<this.data.result.length;i++) {
			if (this.data.result[i]) {
				number++
				// console.log(number)
				this.setData({
					checkNumber: number
				})
			}
			if (number == this.data.cartList.length && number != 0) {
				this.setData({
					checked: true
				})
			} else {
				this.setData({
					checked: false
				})
			}
		}
	},
	checkPrice () {
		let price = 0
		//console.log(this.data.index)
		// console.log(index)
		for (let i=0;i<this.data.result.length;i++) {
			//console.log(this.data.result[i])
			// console.log(this.data.cartList[0])
			// if (this.data.result[i]) {
			// 	//console.log(index)
			// 	// console.log(this.data.result[i])
			// 	this.data.cartList.map((item)=>{
			// 		console.log(item)
			// 		if (this.data.)
			// 	})
			// 	price += this.data.cartList[i].retail_price * this.data.cartList[i].number * 100
			// 	// console.log(this.data.cartList[i])
			// 	// console.log(price)
			// 	this.setData({
			// 		checkPrice: price
			// 	})
			// }
			this.data.cartList.map((item)=>{
				//console.log(item)
				if (this.data.result[i] == item.goods_id) {
					price += item.retail_price * item.number * 100
				}
				//console.log(price)
			})
		}
		this.setData({
			checkPrice: price
		})
	},
	goToDetail (e) {
		let id = e.currentTarget.dataset.id
		// console.log(id)
		wx.navigateTo({
			url: `/pages/goodsdetail/goodsdetail?id=${id}`
		})
	},
	handleLongPress (e) {
		// console.log(e)
		let id = e.currentTarget.dataset.id
		let that = this
		wx.showModal({
			title: '操作提示',
			content: '确认删除该商品?',
			async success(res) {
				if (res.confirm) {
					const data = await post('/cart/delCartGoods', {
						openId: wx.getStorageSync('OPENID') || '',
						goodsId: id
					})
					that.getData()
					console.log(data)
				}
			}
		})
	},
	async settleHandle () {
		let newGoodsId = []
		for (let i=0;i<this.data.result.length;i++) {
			const ele = this.data.result[i]
			if (ele) {
				newGoodsId.push(ele)
			}
		}
		// console.log(newGoodsId)
		let goodsId = newGoodsId.join(',')
		const data = await post('/order/submitAction', {
			openId: wx.getStorageSync('OPENID') || '',
			goodsId: goodsId,
			allPrice: this.data.checkPrice
		})
		// console.log(data)
		if (data) {
			wx.navigateTo({
				url: '/pages/settlepage/settlepage'
			})
		}
	},
	onPageScroll(e) { // page页面的监听滚动的事件
		// console.log(e, 1111)  
		if (e.scrollTop > 100) { // 如果滚动大于100 就显示top组件
			this.setData({
				topIconIsShow: true
			});
		} else {
			this.setData({
				topIconIsShow: false
			});
		}
	}
})
